package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.entity.statistics.LoanAdsChannelDetail;
import com.tuanzi.loan.business.service.statistics.AdverChannelTypeUserService;
import com.tuanzi.loan.business.service.statistics.LoanAdsChannelDetailService;
import com.tuanzi.loan.vo.response.collaborate.CollaborateAdvertiseConfigInfoResponse;
import com.tuanzi.loan.web.bean.CurrentUserBean;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * User: liangZongXi
 * Create time: 16:27 2018/9/18
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateAdvertiseConfigInfoBean extends JSFInfoBean<CollaborateAdvertiseConfigInfoResponse>{
    private static final long serialVersionUID = 1676062256903126221L;

    @Autowired
    private AdverChannelTypeUserService adverChannelTypeUserService;

    @Autowired
    private LoanAdsChannelDetailService loanAdsChannelDetailService;
    @Autowired
    private CurrentUserBean currentUserBean;

    private List<AdverChannelTypeUser> adsChannels;

    private List<LoanAdsChannelDetail> selectChannelLists;

    @PostConstruct
    private void init(){
        String idStr = getParamFromRequest("id");
        initChannelLists();
        if (StringUtils.isNotBlank(idStr)) {
            Long id = Long.valueOf(idStr);
            adsChannels = adverChannelTypeUserService.findAccount(id);
            if(adsChannels != null && adsChannels.size() > 0){
                AdverChannelTypeUser user = adsChannels.get(0);
                Set<String> channels = adsChannels.stream().map(AdverChannelTypeUser::getChannel).collect(Collectors.toSet());
                $ = new CollaborateAdvertiseConfigInfoResponse(user.getId(),user.getUserName(),channels,user.getAdsPlatformType());
            }
        } else {
            $ = new CollaborateAdvertiseConfigInfoResponse();
            changeStatusToAdd();
        }
    }
    protected void initChannelLists(){
        selectChannelLists = loanAdsChannelDetailService.findAllChannel();
    }

    public void save() {
        if($.getChannels() == null || $.getChannels().size() <= 0){
            addWarnMessage("请配置广告渠道");
            return;
        }
        adverChannelTypeUserService.delete($.getAccount());
        Map<String,Integer> map = selectChannelLists.stream().collect(Collectors.toMap(LoanAdsChannelDetail::getChannelCode,LoanAdsChannelDetail::getAdsPlatformType, (v1, v2)-> v1));

        $.getChannels().stream().forEach(x ->{
            adverChannelTypeUserService.saveOrUpdate(new AdverChannelTypeUser(null,$.getAccount(),x,map.get(x)));
        });
        changeStatusToView();
        addSuccessfullyMessage();
    }

    public String back(){
        return redirect(currentUserBean.getBack());
    }
    public void onCopy() {
        $.setId(null);
        changeStatusToEdit();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.ADS_PLATFORM_TYPE);
    }
}
